package com.example.a80205050.myapplication.MyActivity;

import android.accounts.AccountManager;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    GridView gvInfo;
    String[] titles=new String[]{"新增支出","新增收入","我的支出","我的收入",
    "数据管理","系统设置","收支便签","退出"};
    int[] imags=new int[]{R.drawable.addoutaccount,R.drawable.addinaccount,R.drawable.outaccountinfo,R.drawable.inaccountinfo,
    R.drawable.showinfo,R.drawable.sysset,R.drawable.accountflag,R.drawable.exit};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gvInfo=(GridView)findViewById(R.id.gvInfo);
        PictureAdapter pictureAdapter=new PictureAdapter(titles,imags,this);
        gvInfo.setAdapter(pictureAdapter);
        gvInfo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent=null;
                switch (i){
                    case 0:
                        intent=new Intent(MainActivity.this,AddOutaccount.class);
                        startActivity(intent);
                        break;
                    case 1:
                        intent=new Intent(MainActivity.this,AddInaccount.class);
                        startActivity(intent);
                        break;
                    case 2:
                        intent=new Intent(MainActivity.this,Outaccountinfo.class);
                        startActivity(intent);
                        break;
                    case 3:
                        intent=new Intent(MainActivity.this,Inaccountinfo.class);
                        startActivity(intent);
                        break;
                    case 4:
                        intent=new Intent(MainActivity.this,Showinfo.class);
                        startActivity(intent);
                        break;
                    case 5:
                        intent=new Intent(MainActivity.this,Sysset.class);
                        startActivity(intent);
                        break;
                    case 6:
                        intent=new Intent(MainActivity.this,Accountflag.class);
                        startActivity(intent);
                        break;
                    default:
                       finish();

                }
            }
        });

    }
}
class PictureAdapter extends BaseAdapter{
    private LayoutInflater layoutInflater;
    List<Picture>  pictureList;
    public PictureAdapter(String[] title, int[] images, Context context) {
        pictureList=new ArrayList<Picture>();
        layoutInflater=LayoutInflater.from(context);
        for(int i=0;i<images.length;i++){
            Picture picture=new Picture(title[i],images[i]);//初始化图片对象
            pictureList.add(picture);
        }
    }

    @Override
    public int getCount() {
        if(null!=pictureList){
            return  pictureList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return pictureList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if(view==null){
            view=layoutInflater.inflate(R.layout.gvitem,null);
            viewHolder=new ViewHolder();
            viewHolder.title=(TextView)view.findViewById(R.id.ItemTitle);
            viewHolder.imageView=(ImageView)view.findViewById(R.id.ItemImage);
            view.setTag(viewHolder);
        }else{
            viewHolder=(ViewHolder)view.getTag();
        }
        viewHolder.title.setText(pictureList.get(i).getTitle());
        viewHolder.imageView.setImageResource(pictureList.get(i).getImageId());
        return view;
    }
}
class ViewHolder{
    public TextView title;
    public ImageView imageView;
}
class Picture{
    private String title;
    private int imageId;
    public Picture() {
    }
    public Picture(String title, int imageId) {
        this.title = title;
        this.imageId = imageId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getImageId() {
        return imageId;
    }

    public void setImageId(int imageId) {
        this.imageId = imageId;
    }
}

